package com.eqdd.yiqidian.ui.gongneng.renshi.train;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.CareerListResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditChooseBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditTimeBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectPostMultiActivity;
import com.eqdd.yiqidian.ui.select.SelectSingleStaffActivity;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import io.reactivex.Flowable;

public class AddTrainNoticeActivity extends SlimBaseMultiEditActivity {
    private RecyclerViewCustom dataBinding;
    private String msg;
    private String contracts;
    private ArrayList<CareerListResultBean> careerListBeans;
    private ActionSheetDialog numDialog;
    private StringBuilder stringBuilderDepId;
    private StringBuilder stringBuilderDepName;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "培训通知");
        initTopRightText("提交", v -> {
            if (checkParams()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.ADD_TRAIN_NOTICE)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("comid", AppConstent.user.getCompanyId())
                        .params("thePlanId", setParam(0))
                        .params("theplace", setParam(1))
                        .params("teacherGuid", setParam(2))
                        .params("teacherName", setParam(3))
                        .params("teacherInfo", contracts)
                        .params("aSyllabus", setParam(4))
                        .params("presetReleaseTime", setParam(5))
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.REFRESH);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParams() {
        for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
            if (slimAdapterEx.getDataItem(i).isNeed()) {
                if (isParamBlank(i)) {
                    msg = "请输入" + slimAdapterEx.getDataItem(i).getTitle().replace("*", "");
                    return false;
                }
            }
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 0:
                if (numDialog == null) {
                    SingleChooseUtil.getChooseDialog(this, 42, sheetDialog -> {
                        numDialog = sheetDialog;
                        numDialog.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    numDialog.show();
                }
                break;
            case 2:
                startActivityForResult(SelectPostMultiActivity.class, null, Config.SELECT_POST_MULTI);
                break;
            case 4:
                startActivityForResult(SelectSingleStaffActivity.class, null, Config.SELECT_SINGLE_STAFF);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        isSave = false;
        isRead = false;
        datas.add(new SlimEditEnterBean("*培训计划", "", 0, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*培训主题", "", 1, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*受训部门", "", 1, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*受训对象", "", 2, true, "请选择").setNeed(true));
        datas.add(new SlimEditHintBean("*受训人数人数", "", 3).setNeed(true));
        datas.add(new SlimEditHintBean("*培训地点", "", 3).setNeed(true));
        datas.add(new SlimEditEnterBean("*培训时间", "", 4, true, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*主讲老师", "", 5, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditHintBean("*人均费用", "", 6).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训开始时间", "", 7, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditTimeBean("*培训结束时间", "", 8, "请输入").setType(Config.YEAR_MONTH_DAY_TYPE).setNeed(true));
        datas.add(new SlimEditChooseBean("学习形式", "", 9, "请输入", new String[]{"线下面授", "线下集体网络", "网络自学"}));

    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Config.SUCCESS && requestCode == Config.SELECT_POST_MULTI) {
            int num = data.getIntExtra(Config.NUM, 0);
            careerListBeans = data.getParcelableArrayListExtra(Config.SELECTED_BEANS);
            slimAdapterEx.getDataItem(3).setContent(num + "");
            StringBuilder stringBuilder = null;
            for (CareerListResultBean careerListBean : careerListBeans) {
                if (stringBuilder == null) {
                    stringBuilder = new StringBuilder(careerListBean.getName());
                } else {
                    stringBuilder.append(",").append(careerListBean.getName());
                }
            }
            stringBuilderDepId = null;
            stringBuilderDepName = null;
            Flowable.fromIterable(careerListBeans)
                    .distinct(CareerListResultBean::getDepartid)
                    .subscribe(integer -> {
                        if (stringBuilderDepName == null) {
                            stringBuilderDepName = new StringBuilder(integer.getDepartName());
                            stringBuilderDepId = new StringBuilder(integer.getDepartid() + "");
                        } else {
                            stringBuilderDepId.append(",").append(integer.getDepartid());
                            stringBuilderDepName.append(",").append(integer.getDepartName());
                        }
                    });
            slimAdapterEx.getDataItem(2).setContent(stringBuilder.toString());
            slimAdapterEx.notifyItemRangeChanged(2, 2);
            if (!TextUtils.isEmpty(slimAdapterEx.getDataItem(5).getContent())) {
                int totalMoney = Integer.parseInt(setParam(5));
                double avrMoney = totalMoney / num;
                slimAdapterEx.getDataItem(6).setContent(avrMoney + "");
                slimAdapterEx.notifyItemChanged(6);
            }
        } else if (requestCode == Config.SELECT_SINGLE_STAFF && resultCode == Config.INTENT_CONTENT) {
            String content = data.getStringExtra("content");
            contracts = data.getStringExtra(Config.GUID);
            slimAdapterEx.getDataItem(4).setContent(content);
            slimAdapterEx.notifyItemChanged(4);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == 5) {
                if (!TextUtils.isEmpty(setParam(3).trim())) {
                    int totalMoney = Integer.parseInt(data.getStringExtra(Config.CONTENT));
                    int num = Integer.parseInt(setParam(3));
                    double avrMoney = totalMoney / num;
                    slimAdapterEx.getDataItem(6).setContent(avrMoney + "");
                    slimAdapterEx.notifyItemChanged(6);
                }
            }
        }
    }
}
